package Reminder.view.pages;

import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.*;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import Reminder.dao.UserDao;
import Reminder.dao.impl.UserDaoImpl;
import Reminder.domain.Message;
import Reminder.view.Controller;
import Reminder.view.GlobalConfig;
import Reminder.view.Main;

import java.awt.*;
import java.io.IOException;
import java.text.ParseException;
import java.util.Timer;
import java.util.TimerTask;


public class pageLogin extends Page {
//    Scene scene;
//    Controller controller;
    JFXTextField phone = new JFXTextField();
    JFXPasswordField passwordField = new JFXPasswordField();
    boolean lofinFlag = false;
    public pageLogin(Stage stage, Controller controller){
        super(stage,controller);
        try {
            VBox root = new VBox();
            root.setSpacing(80);
            Image icon = new Image("/img/left.png");
            HBox backArrow =  new HBox();
            backArrow.setPrefWidth(100);
            backArrow.setPrefHeight(100);

            ImageView imageView = new ImageView();
            imageView.setFitHeight(25);
            imageView.setFitWidth(25);
            imageView.setImage(icon);

            backArrow.setAlignment(Pos.CENTER_LEFT);
            backArrow.setPadding(new Insets(0,0,0,40));
            backArrow.getChildren().add(imageView);
            backArrow.setOnMouseClicked(
                    new EventHandler<MouseEvent>() {
                        @Override
                        public void handle(MouseEvent mouseEvent) {
                            GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
                            GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
//                            getStage().hide();
                            pageIndex pageIndex = new pageIndex(getStage(),getController());
//                            getStage().show();
                        }
                    }
            );
            backArrow.getStyleClass().add("backIconBar");
            root.getChildren().add(backArrow);
            BorderPane.setAlignment(backArrow,Pos.TOP_RIGHT);

            VBox textFieldBox = new VBox();
            textFieldBox.setPrefWidth(400);
            textFieldBox.setSpacing(40);
            textFieldBox.getStyleClass().add("textFieldBox");

//            textFieldBox.setPrefHeight(300);
//            textFieldBox.setStyle("-fx-background-color:WHITE;");



            phone.setPromptText("请输入手机号");
            phone.setPrefSize(300,40);
            textFieldBox.getChildren().add(phone);



            passwordField.setPromptText("请输入密码");
            textFieldBox.getChildren().add(passwordField);
            textFieldBox.setAlignment(Pos.CENTER);
            textFieldBox.setPrefHeight(200);
            root.getChildren().add(textFieldBox);
            passwordField.setPrefSize(300,40);

            HBox buttonBar = new HBox();
            buttonBar.setPadding(new Insets(60,0,0,0));
            JFXButton login = new JFXButton("登  录");
            login.getStyleClass().add("login");
            login.setPrefSize(250, GlobalConfig.PRIMARY_BUTTON_HEIGHT);
            login.setOnMouseClicked(
                    new EventHandler<MouseEvent>() {
                        @Override
                        public void handle(MouseEvent mouseEvent) {
                        JSONObject form = new JSONObject();
                        form.put("phone", phone.getText());
                        form.put("password",passwordField.getText());
                        UserDao userDao = new UserDaoImpl();
                        try {
                            Message msg = userDao.login(form);
                            if(msg.toJSON().getString("status").equals("success")){
                                GlobalConfig.UserInfo = JSONObject.parseObject(msg.toJSON().getString("data"));
                                GlobalConfig.USER_ID = GlobalConfig.UserInfo.getInteger("userId");
                                GlobalConfig.IS_LOGIN = true;
                                System.out.println(msg);
                                getController().displayTray("登录成功", "", "info");
                                toOption();
                            }
                            else{
                                getController().displayTray("登录失败",msg.toJSON().getString("data"),"error");
                            }
                        } catch (IOException | AWTException | ParseException e) {
                            e.printStackTrace();
                        }
                        }
                    }
            );

            JFXButton register = new JFXButton("注  册");
            register.setPrefSize(250,GlobalConfig.PRIMARY_BUTTON_HEIGHT);
            register.getStyleClass().add("register");
            register.setOnMouseClicked(
                    new EventHandler<MouseEvent>() {
                        @Override
                        public void handle(MouseEvent mouseEvent) {
                            JSONObject form = new JSONObject();
                            form.put("phone", phone.getText());
                            form.put("password",passwordField.getText());
                            UserDao userDao = new UserDaoImpl();
                            try {
                                Message msg = userDao.register(form);
                                if(msg.toJSON().getString("status").equals("success")){
                                    System.out.println("---test--2342-" + msg);
                                    getController().displayTray("注册成功"," ","info");

//                                    toOption();
                                }
                                else{
                                    getController().displayTray("注册失败","","error");
                                }
                            } catch (IOException | AWTException | ParseException e) {
                                e.printStackTrace();
                            }
                        }
                    }
            );

            buttonBar.setSpacing(40);
            buttonBar.setPrefHeight(100);
            buttonBar.getChildren().addAll(login,register);
            buttonBar.setAlignment(Pos.CENTER);


            textFieldBox.getChildren().add(buttonBar);

            root.getStyleClass().add("root");

            Scene scene = new Scene(root, GlobalConfig.WINDOW_CURRENT_WIDTH, GlobalConfig.WINDOW_CURRENT_HEIGHT, Color.WHITE);
            scene.getStylesheets().add(Main.class.getResource("/css/login.css").toExternalForm());
            stage.setScene(scene);

        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public void toOption() throws IOException, ParseException, AWTException {
        GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
        GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            pageOptions pageOptions = new pageOptions(getStage(),getController());
                        } catch (IOException | ParseException | AWTException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        };
        timer.schedule(timerTask,500);

    }


}
